//
//  PlayerRightView.swift
//

import UIKit

class PlayerRightView: UIView {
    
    /// 选择的item
    var didSelectedItem: (Int) -> Void = { _ in }
    
    var isShow: Bool = false {
        didSet {
            if isShow {
                UIView.animate(withDuration: 0.25, animations: {
                    self.transform = CGAffineTransform(translationX: 170.fit, y: 0)
                    self.alpha = 0
                }) {_ in}
            } else {
                transform = CGAffineTransform(translationX: 170.fit, y: 0)
                UIView.animate(withDuration: 0.25) {
                    self.alpha = 1
                    self.transform = .identity
                }
            }
        }
    }
    
    /// 列表数据
    var items: [RecChildDataItemsModel] = [] {
        didSet {
            tableView.reloadData()
        }
    }
    
    private lazy var tableView: UITableView = {
        let view = UITableView(frame: .zero, style: .plain)
        view.rowHeight = 150.fit * 3 / 4
        view.delegate = self
        view.dataSource = self
        view.backgroundColor = UIColor.clear
        view.separatorStyle = .none
        view.register(cellWithClass: PlayerRightCell.self)
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.black.alpha(0.3)
        addSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addSubviews() {
        addSubview(tableView)
        
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }

}

extension PlayerRightView: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: PlayerRightCell.self)
        cell.iconView.kf_set(items[indexPath.row].image)
        cell.titleLabel.text = items[indexPath.row].title
        cell.bgView.backgroundColor = items[indexPath.row].isSelected ? UIColor.theme : UIColor(rgb: 102, green: 167, blue: 241)
        cell.titleLabel.backgroundColor = items[indexPath.row].isSelected ? UIColor(rgb: 250, green: 238, blue: 167) : UIColor(rgb: 220, green: 237, blue: 251)
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        didSelectedItem(indexPath.row)
    }
}

class PlayerRightCell: UITableViewCell {
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.corner(radii: 8.fit)
        return view
    }()
    
    lazy var bgView1: UIView = {
        let view = UIView()
        view.corner(radii: 8.fit)
        view.backgroundColor = UIColor.white
        return view
    }()
    
    lazy var iconView: UIImageView = {
        let view = UIImageView()
        view.corner(radii: 6.fit)
        return view
    }()
    
    lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.black
        view.font = 13.font
        view.textAlignment = .center
        return view
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        backgroundColor = UIColor.clear
        contentView.addSubview(bgView)
        bgView.addSubview(bgView1)
        bgView1.addSubview(iconView)
        iconView.addSubview(titleLabel)
        
        bgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10.fit)
            make.left.equalToSuperview().offset(10.fit)
            make.right.equalToSuperview().offset(-10.fit)
            make.bottom.equalToSuperview()
        }
        
        bgView1.snp.makeConstraints { (make) in
            make.top.left.equalToSuperview().offset(4.fit)
            make.right.bottom.equalToSuperview().offset(-4.fit)
        }
        
        iconView.snp.makeConstraints { (make) in
            make.top.left.equalToSuperview().offset(2.fit)
            make.right.bottom.equalToSuperview().offset(-2.fit)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.height.equalTo(20.fit)
            make.left.right.bottom.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
